package server;

import java.io.*;

public class Response {
    private OutputStream out;

    public Response(OutputStream out) {
        this.out = out;
    }

    /**
     * 发送字符串文本信息，到客户端
     *
     * @param str 要发送的信息
     */
    public void sendMessage(String str) {
        try {
            out.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送文件信息到客服端
     *
     * @param filePath 文件路径
     */
    public void sendFile(String filePath) {
        File f = new File(filePath);
        if (f.exists() == false) {//判断文件是否存在
            return;
        }
        InputStream in = null;
        try {
            in = new FileInputStream(f);
            byte[]by=new byte[1024];
            int len=0;
            //将数据读取到字节数组，返回当前字节到字节数
            while ((len=in.read(by))!=-1){
                out.write(by,0,len);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
